<template>
  <div class="login_container">
    <div class="login_form">
      <el-form class="form_box" :model="form" :rules="rules" ref="form_ref" status-icon>
        <h1>WELCOME</h1>
        <h2>欢迎登录。。。</h2>
        <el-form-item prop="username">
          <el-input v-model="form.username" :prefix-icon="User" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" :prefix-icon="Lock" show-password placeholder="请输入密码" type="password"></el-input>
        </el-form-item>
        <el-form-item class="btn_box">
          <el-button :loading="loading" type="primary" @click="handleSubmit">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, onBeforeMount } from "vue";
import { User, Lock } from "@element-plus/icons-vue";
import { ElNotification } from "element-plus";
import useUserStore from "@/store/modules/user";
import { useRouter, useRoute } from "vue-router";
import { getNowTime } from "@/utils/util";

onBeforeMount(() => {});

// 初始化用户信息仓库
const userStore = useUserStore();
const $route = useRoute();
const $router = useRouter();
// 定义表单数据
const form = ref({
  username: "admin",
  password: "111111"
});
// 定义按钮加载动画
const loading = ref(false);
// 用于获取表单ref
const form_ref = ref();
// 定义表单校验规则
// const validateUsername = (rule: any, value: any, callback: any) => {
//   console.log(rule, value, callback);
// };
// const validatePassword = (rule: any, value: any, callback: any) => {
//   console.log(rule, value, callback);
// };
// const rules = {
//   username: [{ validator: validateUsername, trigger: "blur" }],
//   password: [{ validator: validatePassword, trigger: "change" }]
// };
const rules = {
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, min: 6, max: 10, message: "密码为6~10位", trigger: "change" }]
};
// console.log($route);
// 用户点击登录按钮
const handleSubmit = async () => {
  // console.log(form);
  loading.value = true;
  await form_ref.value.validate();
  // 发送请求
  userStore
    .userLogin(form.value)
    .then(() => {
      // 请求成功 跳转首页 提示成功
      ElNotification({
        type: "success",
        title: `HI ${getNowTime()}好！`,
        message: "登录成功！"
      });
      const redirect: any = $route.query.redirect;
      if (redirect) {
        $router.push(redirect);
      } else {
        $router.push("/");
      }

      loading.value = false;
    })
    .catch((err: Error) => {
      // 请求失败 提示失败
      ElNotification({
        type: "error",
        title: "登录失败",
        message: err.message
      });
      loading.value = false;
    });
};
</script>

<style scoped lang="scss">
.login_container {
  background-color: skyblue;
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
.login_form {
  width: 600px;
  height: 400px;
  border: 1px solid yellow;
  border-radius: 10px;
  padding: 20px;
  box-sizing: border-box;
  .form_box {
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
  }
  .btn_box {
    button {
      width: 100%;
    }
  }
  h1 {
    color: white;
    font-size: 40px;
  }
  h2 {
    color: white;
    font-size: 20px;
  }
}
</style>
